//
//  InfoTypeView.m
//  WanCaiC8
//
//  Created by tangang on 2018/6/8.
//  Copyright © 2018年 tangang. All rights reserved.
//

#import "InfoTypeView.h"
#import "LabCell.h"
#import "NewsLottryTypeModel.h"
#import "OpenLotteryRecordVC.h"
#import "InformationVC.h"
#import "PlanVC.h"
#import "MasterListVC.h"
#import "LotteryNavModel.h"
@interface InfoTypeView()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property (nonatomic ,strong) UICollectionView *collectionView;

@end

@implementation InfoTypeView



- (instancetype)init{
    if (self=[super init]) {
        self.backgroundColor=[UIColor whiteColor];
    }
    return self;
}

- (void)setDataArr:(NSMutableArray *)dataArr{
    _dataArr=dataArr;
    if (dataArr.count<8) {
        [_dataArr insertObject:[self getModelWithTitle:@"开奖历史"] atIndex:0];
        [_dataArr insertObject:[self getModelWithTitle:@"官方计划"] atIndex:1];
        [_dataArr insertObject:[self getModelWithTitle:@"高手计划"] atIndex:2];
    }
    [self.collectionView reloadData];
}

- (NewsTypeModel *)getModelWithTitle:(NSString *)title{
    NewsTypeModel *model=[[NewsTypeModel alloc]init];
    model.TypeName=title;
    return model;
}

- (void)setScrollIndex:(NSInteger)scrollIndex{
    
    if (self.dataArr.count==0) {
        return;
    }
    
    [self layoutIfNeeded];
    [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:scrollIndex inSection:0] atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
}

#pragma mark - collectionView代理函数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataArr.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    LabCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"LabCell" forIndexPath:indexPath];
    NewsTypeModel *model=self.dataArr[indexPath.row];
    
    BOOL isShow=((indexPath.row)%4==0);
    
    if ([model isKindOfClass:[LotteryNavModel class]]) {
        LotteryNavModel *lottModel=(LotteryNavModel *)model;
        [cell setAllLineTitle:lottModel.Name andShowRightLine:!isShow andShowBottomLine:((self.dataArr.count-indexPath.row)>4)];
    }else{
        [cell setAllLineTitle:model.TypeName andShowRightLine:!isShow andShowBottomLine:((self.dataArr.count-indexPath.row)>4)];
    }
//    [cell setBtnTitle:model.TypeName andIsSelect:NO];
//    [cell.labTitle setFont:[UIFont systemFontOfSize:15]];
    return cell;
}


//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
//    return CGSizeMake((SCREEN_WIDTH-20)/4, 25);
//}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, 0, 0, 0);
}

//- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
//    return 10;
//}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NewsTypeModel *model=self.dataArr[indexPath.row];
    UIViewController *selfVC=[TGTools viewController:self];
    
    if ([model isKindOfClass:[LotteryNavModel class]]) {
        LotteryNavModel *lModel=(LotteryNavModel *)model;
        self.selectNav(lModel);
    }else{
        if (indexPath.row==0) {
            OpenLotteryRecordVC *vc =[[OpenLotteryRecordVC alloc]init];
            vc.lType=self.lType;
            vc.title=[NSString stringWithFormat:@"%@开奖记录",self.name];
            vc.lotteryType=[[BaseVM manage] getType:[model.LType integerValue]];
            [selfVC.navigationController pushViewController:vc animated:YES];
        }else if (indexPath.row==1){
            PlanVC *vc=[[PlanVC alloc]init];
            LotteryTypeModel *typeModel=[[LotteryTypeModel alloc]init];
            typeModel.lType=self.lType;
            typeModel.Name=self.name;
            vc.lotteryModel=typeModel;
            [[TGTools viewController:self].navigationController pushViewController:vc animated:YES];
        }else if (indexPath.row==2){
            
            //进入高手计划
            MasterListVC *vc=[[MasterListVC alloc]init];
            vc.lType=self.lType;
            vc.strTitle=self.name;
            [[TGTools viewController:self].navigationController pushViewController:vc animated:YES];
        }else{
            InformationVC *vc=[[InformationVC alloc]init];
            vc.lType=self.lType;
            vc.index=indexPath.row-3;
            vc.title=self.name;
            vc.saveViewController=selfVC;
            [selfVC.navigationController pushViewController:vc animated:YES];
        }
    }
    
}

- (UICollectionView *)collectionView{
    if (!_collectionView) {
        WS(weakSelf);
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        [layout setItemSize:CGSizeMake((SCREEN_WIDTH-real_size(20))/4, real_size(40))];
        [layout setScrollDirection:UICollectionViewScrollDirectionVertical];
        layout.minimumInteritemSpacing = 0.0;
        layout.minimumLineSpacing = 0.0;
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.showsHorizontalScrollIndicator=NO;
        [_collectionView registerClass:[LabCell class] forCellWithReuseIdentifier:@"LabCell"];
        [self addSubview:_collectionView];
        [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(weakSelf).with.insets(UIEdgeInsetsMake(real_size(10), real_size(10), 0, real_size(10)));
        }];
    }
    return _collectionView;
}
@end
